#include "stdafx.h"
#include "IBitOr.h"
//OpenCV libs
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"

const char * const IBitOr::FunctionNameIBitOr = "Bitwise-OR";

IBitOr::IBitOr(void) 
{
	_fnApi.SetFunctionName(IBitOr::FunctionNameIBitOr);
	_fnApi.SetFunctionDescription("Performs bitwise OR with a selected image");
									//inputs
	_fnApi.AddParamDef(new ProcParamDefString(0, "Image", "Sets the Image to be used. Make sure the dimensions are the same as image from Image Curator", ProcParamDef::input, 0, 500 ));
	_fnApi.AddParamDef(new ProcParamDefString(1, "Mask", "Sets the area to be used. Make sure the dimensions are the same as image from Image Curator", ProcParamDef::input, 0, 500 ));
									//outputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, ProcParam::attribOutputParamNameResult, "0 is success", ProcParamDef::output, 0, 1));

	_fnApi.Validate();
}


IBitOr::~IBitOr(void)
{

}


bool IBitOr::Process(ImageProcStep *step, bool DebugMode)
{	
	bool rc = false;
	if ((step != '\0') && (step->IsValid()))
	{
		
		ProcParamString *source = dynamic_cast<ProcParamString *>(step->GetInputs()->GetParam(0));	//Check the Param number
		ProcParamString *masksource = dynamic_cast<ProcParamString *>(step->GetInputs()->GetParam(1));
		if  (source != '\0' && masksource != '\0')
		{
			cv::Mat output;
			cv::Mat input = cv::imread(step->GetIntermediateImagePathFilename());	
			cv::Mat input2 = cv::imread((std::string)source->GetValueStr());
			cv::Mat mask = cv::imread((std::string)masksource->GetValueStr(), 0);		//needs to be single channel

			if (input.cols == input2.cols && input.rows == input2. rows)
			{
				cv::bitwise_or(input, input2, output, mask);

				cv::imwrite(step->GetIntermediateImagePathFilename(), output);
				
				input.release();
				input2.release();
				mask.release();
				output.release();
				rc = true;
			}
		}
	}
	return rc;
}

